package com.lsh.airticket.controller;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.StrUtil;
import com.lsh.airticket.dto.LoginUser;
import com.lsh.airticket.dto.Result;
import com.lsh.airticket.dto.UserInfo;
import com.lsh.airticket.entity.User;
import com.lsh.airticket.exception.UserException;
import com.lsh.airticket.service.UserService;
import com.lsh.airticket.util.JWTUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * @author lsh
 * @date 2021/2/13
 **/
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 用户登录方法
     * @param loginUser
     * @return
     */
    @PostMapping("/user/login")
    public Result login(@RequestBody LoginUser loginUser) {
        System.out.println(loginUser);
        User user = userService.login(loginUser.getUsername(), loginUser.getPassword());
        if (user == null) {
            return Result.result().setMsg("用户名或密码错误").setStatus(2);
        }
        System.out.println(user);
        HashMap<String, Object> map = new HashMap<>();
        map.put("userTel", user.getUserTel());
        //添加标志位 1 管理员 2表示普通用户
        map.put("flag", 2);
        //生成token
        String token = JWTUtil.create(map);
        //token 用base64编码
        token = Base64.encode(token);
        UserInfo userInfo = new UserInfo();
        BeanUtils.copyProperties(user, userInfo);
        userInfo.setFlag(2);
        return Result.result().setMsg("登录成功").addData("token", token).addData("userInfo", userInfo);
    }

    /**
     * 手机号登录
     * @param loginUser
     * @return
     */
    @PostMapping("/user/smsLogin")
    public Result smsLogin(@RequestBody LoginUser loginUser) {
        System.out.println(loginUser);
        if (loginUser == null || StrUtil.isEmpty(loginUser.getUsername()) || StrUtil.isEmpty(loginUser.getCode())) {
            throw new UserException("登录失败");
        }
        User user = userService.findUserByPhone(loginUser.getUsername());

        System.out.println(user);
        String code = (String) redisTemplate.opsForValue().get(loginUser.getUsername());
        if (StrUtil.isEmpty(code) && !code.equals(loginUser.getCode())) {
            throw new UserException("验证码错误");
        }

        HashMap<String, Object> map = new HashMap<>();
        map.put("userTel", user.getUserTel());
        //添加标志位 1 管理员 2表示普通用户
        map.put("flag", 2);
        //生成token
        String token = JWTUtil.create(map);
        //token 用base64编码
        token = Base64.encode(token);
        UserInfo userInfo = new UserInfo();
        BeanUtils.copyProperties(user, userInfo);
        userInfo.setFlag(2);
        return Result.result().setMsg("登录成功").addData("token", token).addData("userInfo", userInfo);
    }

    @PostMapping("/user/register")
    public Result register(@RequestBody User user) {
        if (user == null) {
            throw new UserException("不能为空");
        }
        int insert = userService.register(user);
        if (insert != 1) {
            throw  new UserException("注册失败");
        }
        return Result.result().setMsg("注册成功");
    }

    /**
     * 验证手机号是否被注册
     * @param userTel
     * @return
     */
    @GetMapping("/user/findTel")
    public Result findTel(String userTel) {
        if (StrUtil.isEmpty(userTel)) {
            throw new UserException("手机号不能为空");
        }
        userService.findUserByTel(userTel);
        return Result.result();
    }

    /**
     * 根据用户id查找用户信息。
     * @param userId
     * @return
     */
    @GetMapping("/user/findTbUserById")
    public User findTbUserById(Integer userId) {
        // User user = userService.findTbUserById(userId);
        User user = new User();
        return user;
    }



}
